package com.sjsoft.webservice;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

import com.sjsoft.constant.Constant;
import com.sjsoft.interfaces.OnUploadingCompleteListener;
import com.sjsoft.interfaces.OnUploadingStartListener;
import com.sjsoft.model.IRequestItem;
import com.sjsoft.model.messageItem;
import com.sjsoft.model.videoItem;

import android.content.Context;


public class ServiceQueueUploadingVideo implements Constant{
	public static Queue<IRequestItem> downloadQueue = new LinkedList<IRequestItem>();
	public static GetVideoThread download;
	public static final String MESSAGE_ERROR = "Error";
	public static final String MESSAGE_COMPLETE = "Success";
	private boolean downloadState = false;
	public static int statusCode;
	public static final int NETWORK_STATUS_OK = 0;
	public static final int NETWORK_STATUS_OFF = 1;
	public static final int NETWORK_STATUS_ERROR = 2;
	private static OnUploadingCompleteListener onGetDataCompleteListener;
	private static OnUploadingStartListener onGetDateStartListener;
	
	
	public static void pushDownloadTaskQueue(Context context, IRequestItem url) {
		synchronized (downloadQueue) {
			downloadQueue.add(url);
		}
	}

	public static void runDownloadQueue(Context context) {
		if (!downloadQueue.isEmpty()) {
			try {
				synchronized (downloadQueue) {
					IRequestItem item = downloadQueue.remove();
					download = new GetVideoThread(context, item);
					download.setonGetDataCompleteListener(onGetDataCompleteListener);
					download.setOnGetDataStartListener(onGetDateStartListener);
				}
			} catch (Exception e) {
				e.printStackTrace();
				cancelTask();
			}
		}

	}

	public static void cancelTask() {
		download.onCancelled();
	}

	public static void cancelAll() {
		if (download != null)
			download.onCancelledAll();
	}

	public static ArrayList<videoItem> requestVideo(String extendId) {
		String json =  UtilsJson.getItemsVideo(extendId,0,4);
		ArrayList<videoItem> list = UtilsJson.getListVideoFromJson(json);
		int nSize = list.size();
		for(int i = 0; i< nSize; i++)
		{
			videoItem item = list.get(i);
			String jsonMsg = UtilsJson.getListChat(item.getProfileId());
			item = UtilsJson.getListMessageFromJson(jsonMsg,item);
			//item.setMessage(arrMsg);
			list.set(i, item);
		}
		return list;
		
}
	public static OnUploadingCompleteListener getonGetDataCompleteListener() {
		return onGetDataCompleteListener;
	}

	public static void setonGetDataCompleteListener(
			OnUploadingCompleteListener onGetDataCompleteListener) {
		ServiceQueueUploadingVideo.onGetDataCompleteListener = onGetDataCompleteListener;
	}

	public static OnUploadingStartListener getonGetDateStartListener() {
		return onGetDateStartListener;
	}

	public static void setonGetDateStartListener(
			OnUploadingStartListener onGetDateStartListener) {
		ServiceQueueUploadingVideo.onGetDateStartListener = onGetDateStartListener;
	}

	public boolean downloadState() {
		return downloadState;
	}
}
